import os
import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from common.functions import open_browser
from selenium import webdriver
from selenium.common import NoSuchElementException, WebDriverException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Base:
    def __init__(self, browser):
        self.driver = browser
        self.driver.implicitly_wait(5)
        # self.driver.maximize_window()

    # 获取网址
    def get(self, url):
        try:
            self.driver.get(url)
        except WebDriverException:
            print('网址错误')

    # 窗口最大化
    def max_window(self):
        self.driver.maximize_window()

    def max_wind(self):
        self.driver.maximize_window()

    # 获取元素
    def find_element(self, located, timeout=5):
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(located))
            # return self.driver.find_element(*located)
        except TimeoutException:
            print('没有定位到元素')

    # 鼠标悬停
    def move_to_element(self, located):
        ac = ActionChains(self.driver)
        ele = self.find_element(located)
        ac.move_to_element(ele).perform()

    # 清空输入框
    def clear(self, located):
        try:
            self.find_element(located).clear()
        except Exception as e:
            print(e)

    # 弹窗获取
    def alert(self):
        self.driver.switch_to.alert.accept()

    # select类
    def select(self, located, index):
        s1 = self.find_element(located)
        select = Select(s1)
        select.select_by_index(index)

    def order_status(self, locator, value):
        s1 = self.find_element(locator)
        select = Select(s1)
        select.select_by_value(value)

    def select_by_index(self, locator, index):
        ele = self.find_element(locator)
        select = Select(ele)
        select.select_by_index(index)

    def select_by_text(self, locator, text):
        ele = self.find_element(locator)
        select = Select(ele)
        select.select_by_visible_text(text)

    # 切换iframe
    def iframe(self, located=True, frame='default'):
        if frame == 'default':
            self.driver.switch_to.default_content()
        elif frame == 'switch':
            fn = self.find_element(located)
            self.driver.switch_to.frame(fn)
        elif frame == 'parent':
            self.driver.switch_to.parent_frame()

    def frame(self, locator):
        frame = self.find_element(locator)
        self.driver.switch_to.frame(frame)

    # 切换窗口
    def switch_windows(self, num):
        win = self.driver.window_handles
        self.driver.switch_to.window(win[num])

    # 输入内容
    def send_keys(self, located, content):
        self.find_element(located).send_keys(content)

    # 获取属性值
    def get_attribute(self, located, attr='outerHTML'):
        return self.find_element(located).get_attribute(attr)

    # 平滑滚动
    def execute_script(self):
        js3 = "window.scrollTo({left:0,top:document.body.scrollHeight,behavior:'smooth'})"
        self.driver.execute_script(js3)

    def browser_smooth(self):
        js = "window.scrollTo({left:0,top:document.body.scrollHeight,behavior:'smooth'})"
        self.driver.execute_script(js)

    # 上滑滚动
    def slide_up(self):
        js = "window.scrollTo({left:0,top:0,behavior:'smooth'})"
        self.driver.execute_script(js)

    # 浏览器后退
    def back(self):
        self.driver.back()

    # 浏览器前进
    def forward(self):
        self.driver.forward()

    # 浏览器刷新
    def refresh(self):
        self.driver.refresh()

    # 关闭当前窗口
    def close(self):
        self.driver.close()

    # 点击
    def click(self, located):
        element = self.find_element(located)
        if element is not None:
            element.click()
        else:
            print('没有找到元素')

    # 双击
    def dou_ble(self, located):
        dou = self.find_element(located)
        ac = ActionChains(self.driver)
        ac.double_click(dou).perform()

    # 获取元素文本
    def get_element_text(self, locator):
        element = self.find_element(locator)
        if element:
            return element.text

    # 退出浏览器
    def quit(self, second=3):
        time.sleep(second)
        self.driver.quit()


if __name__ == '__main__':
    browser1 = open_browser()
    bs = Base(browser1)
    bs.get('https://www.baidu.com')
    # t1 = (By.ID, 'kw')
    # bs.find_element((By.ID, 'kw'))
    bs.get_attribute((By.ID, 'kw'))
    bs.quit()
